﻿Imports System.Drawing.Design
Imports System.Windows.Forms.Design
Imports System.Windows.Forms
Imports System
Imports System.Collections.Generic

Public Class NIB_SystemTypePropertyEditor
   Inherits UITypeEditor
   Private WithEvents myListBox As New ListBox

   'Get an IWindowsFormsEditorService object.
   Dim editorservice As IWindowsFormsEditorService = Nothing

   Public Overrides Function EditValue( _
      ByVal context As System.ComponentModel.ITypeDescriptorContext, _
      ByVal provider As System.IServiceProvider, _
      ByVal value As Object) As Object

      If (provider IsNot Nothing) Then
         editorservice = _
            CType(provider.GetService(GetType(IWindowsFormsEditorService)),  _
            IWindowsFormsEditorService)
      End If

      If editorservice IsNot Nothing Then

         ' Make the editing control.
         Dim editorcontrol As ListBox = New ListBox
         AddHandler editorcontrol.SelectedIndexChanged, AddressOf Me.ClickSelect

         With editorcontrol
            .Items.Clear()

            Dim nib As Object = System.Activator.CreateInstance(Type.GetType("TnTinMn.NIB.NumericInputBox", True, True))
            Dim fi As Reflection.FieldInfo
            fi = Type.GetType("TnTinMn.NIB.NumericInputBox", True, True).GetField("AllowedTypes", _
                             Reflection.BindingFlags.GetField Or Reflection.BindingFlags.Static Or _
                             Reflection.BindingFlags.NonPublic)
            Dim AllowedTypes As Type() = CType(fi.GetValue(nib), Type())


            For Each t As Type In AllowedTypes 'NumericInputBox.AllowedTypes
               .Items.Add(t)
            Next
            .AllowDrop = False
            .Height = .PreferredHeight

         End With

         ' Display the editing control.
         editorservice.DropDownControl(editorcontrol)

         ' Save the new results.
         If editorcontrol.SelectedItem IsNot Nothing Then
            Return editorcontrol.SelectedItem
         Else
            Return value
         End If

      End If 'editorservice Isnot Nothing

      Return MyBase.EditValue(context, provider, value)

   End Function ' EditValue

   Public Overrides Function GetEditStyle( _
         ByVal context As System.ComponentModel.ITypeDescriptorContext) _
         As System.Drawing.Design.UITypeEditorEditStyle

      Return UITypeEditorEditStyle.DropDown
   End Function

   Public Sub ClickSelect(ByVal sender As Object, ByVal e As EventArgs)
      editorservice.CloseDropDown()
   End Sub

End Class